﻿using System;
using System.Collections.Generic;
using HslCommunication.LogNet;

namespace HslCommunication.Core
{
	/// <summary>
	/// 文件标记对象类，标记了一个文件的当前状态，是否处于下载中，删除的操作信息<br />
	/// File tag object class, which marks the current status of a file, whether it is downloading, or delete operation information
	/// </summary>
	// Token: 0x02000130 RID: 304
	public class FileMarkId
	{
		/// <summary>
		/// 实例化一个文件标记对象，需要传入日志信息和文件名<br />
		/// To instantiate a file tag object, you need to pass in log information and file name
		/// </summary>
		/// <param name="logNet">日志对象</param>
		/// <param name="fileName">完整的文件名称</param>
		// Token: 0x06001998 RID: 6552 RVA: 0x000803E0 File Offset: 0x0007E5E0
		public FileMarkId(ILogNet logNet, string fileName)
		{
			this.logNet = logNet;
			this.fileName = fileName;
			this.CreateTime = DateTime.Now;
			this.ActiveTime = DateTime.Now;
		}

		/// <summary>
		/// 当前的对象创建的时间<br />
		/// Current object creation time
		/// </summary>
		// Token: 0x17000551 RID: 1361
		// (get) Token: 0x06001999 RID: 6553 RVA: 0x0008043F File Offset: 0x0007E63F
		// (set) Token: 0x0600199A RID: 6554 RVA: 0x00080447 File Offset: 0x0007E647
		public DateTime CreateTime { get; private set; }

		/// <summary>
		/// 当前的对象最后一次活跃的时间<br />
		/// Last active time of the current object
		/// </summary>
		// Token: 0x17000552 RID: 1362
		// (get) Token: 0x0600199B RID: 6555 RVA: 0x00080450 File Offset: 0x0007E650
		// (set) Token: 0x0600199C RID: 6556 RVA: 0x00080458 File Offset: 0x0007E658
		public DateTime ActiveTime { get; private set; }

		/// <summary>
		/// 当前的文件的读取次数，通常也是下载次数。<br />
		/// The current number of reads of the file, usually also the number of downloads.
		/// </summary>
		// Token: 0x17000553 RID: 1363
		// (get) Token: 0x0600199D RID: 6557 RVA: 0x00080461 File Offset: 0x0007E661
		// (set) Token: 0x0600199E RID: 6558 RVA: 0x00080469 File Offset: 0x0007E669
		public long DownloadTimes { get; private set; }

		/// <summary>
		/// 新增一个对当前的文件的操作，如果没有读取，就直接执行，如果有读取，就等待读取完成的时候执行。<br />
		/// Add an operation on the current file. If there is no read, it will be executed directly. If there is a read, it will be executed when the read is completed.
		/// </summary>
		/// <param name="action">对当前文件的操作内容</param>
		// Token: 0x0600199F RID: 6559 RVA: 0x00080474 File Offset: 0x0007E674
		public void AddOperation(Action action)
		{
			object obj = this.hybirdLock;
			lock (obj)
			{
				bool flag2 = this.readStatus == 0;
				if (flag2)
				{
					if (action != null)
					{
						action();
					}
				}
				else
				{
					ILogNet logNet = this.logNet;
					if (logNet != null)
					{
						logNet.WriteDebug(this.ToString(), "action delay");
					}
					this.queues.Enqueue(action);
				}
			}
		}

		/// <summary>
		/// 获取该对象是否能被清除<br />
		/// Gets whether the object can be cleared
		/// </summary>
		/// <returns>是否能够删除</returns>
		// Token: 0x060019A0 RID: 6560 RVA: 0x000804FC File Offset: 0x0007E6FC
		public bool CanClear()
		{
			bool result = false;
			object obj = this.hybirdLock;
			lock (obj)
			{
				result = (this.readStatus == 0 && this.queues.Count == 0);
			}
			return result;
		}

		/// <summary>
		/// 进入文件的读取状态<br />
		/// Enter the read state of the file
		/// </summary>
		// Token: 0x060019A1 RID: 6561 RVA: 0x0008055C File Offset: 0x0007E75C
		public void EnterReadOperator()
		{
			object obj = this.hybirdLock;
			lock (obj)
			{
				this.readStatus++;
				this.ActiveTime = DateTime.Now;
			}
		}

		/// <summary>
		/// 离开本次的文件读取状态，如果没有任何的客户端在读取了，就执行缓存队列里的操作信息。<br />
		/// Leaving the current file reading status, if no client is reading, the operation information in the cache queue is executed.
		/// </summary>
		// Token: 0x060019A2 RID: 6562 RVA: 0x000805B8 File Offset: 0x0007E7B8
		public void LeaveReadOperator()
		{
			object obj = this.hybirdLock;
			lock (obj)
			{
				this.readStatus--;
				long downloadTimes = this.DownloadTimes;
				this.DownloadTimes = downloadTimes + 1L;
				bool flag2 = this.readStatus == 0;
				if (flag2)
				{
					while (this.queues.Count > 0)
					{
						try
						{
							Action action = this.queues.Dequeue();
							if (action != null)
							{
								action();
							}
							ILogNet logNet = this.logNet;
							if (logNet != null)
							{
								logNet.WriteDebug(this.ToString(), "action delay execute");
							}
						}
						catch (Exception ex)
						{
							ILogNet logNet2 = this.logNet;
							if (logNet2 != null)
							{
								logNet2.WriteException("FileMarkId", "File Action Failed:", ex);
							}
						}
					}
				}
				this.ActiveTime = DateTime.Now;
			}
		}

		/// <inheritdoc />
		// Token: 0x060019A3 RID: 6563 RVA: 0x000806B4 File Offset: 0x0007E8B4
		public override string ToString()
		{
			return "FileMarkId[" + this.fileName + "]";
		}

		// Token: 0x040005B5 RID: 1461
		private int readStatus = 0;

		// Token: 0x040005B6 RID: 1462
		private readonly ILogNet logNet;

		// Token: 0x040005B7 RID: 1463
		private readonly string fileName = null;

		// Token: 0x040005B8 RID: 1464
		private readonly Queue<Action> queues = new Queue<Action>();

		// Token: 0x040005B9 RID: 1465
		private readonly object hybirdLock = new object();
	}
}
